<?php

class member extends Controller {
	
	public function index() {
		if((isset($_COOKIE['username']) && isset($_COOKIE['password'])) || (isset($_SESSION['user']['username']) && isset($_SESSION['user']['password']))) {
			if(isset($_SESSION['user']['username']) && isset($_SESSION['user']['password'])) {
				$username = $_SESSION['user']['username'];
				$password = $_SESSION['user']['password'];
			} else {	
				$username = User::perso_decrypt($_COOKIE['username']);
				$password = User::perso_decrypt($_COOKIE['password']);
			}
			$mailbox = Imap::login($username,$password);
			Imap::close($mailbox);
			if (!$mailbox) {
				$this->render('login');
			} else {
				$_SESSION['user']['username'] = $username;
				$_SESSION['user']['password'] = $password;
				$_SESSION['user']['mailboxes'] = Imap::getMailboxes();
				header('Location: '.WEB_ROOT.'webmail/menu');
				exit ();
			}
		} else {
			$this->render('login');
		}
	}
	
	public function login() {
		if(!isset($_POST['login'])) {
			header('Location: '.WEB_ROOT.'webmail/index');
			exit();
		}
		// Pré-traitement du formulaire
		$_POST['login'] = array_map('trim',$_POST['login']);
		$_POST['login'] = array_map('addslashes',$_POST['login']);
		extract($_POST['login']);
		// Validation des champs
		if(empty($username)) { $_SESSION['form'] = $_POST['login']; $_SESSION['errors']['username'] = "Nom d'utilisateur : champs requis"; }
		if(empty($password)) { $_SESSION['form'] = $_POST['login']; $_SESSION['errors']['password'] = "Mot de passe : champs requis"; }
		// Teste si l'utilisateur existe
		if(!isset($_SESSION['form'])) {
			$mailbox = false;
			// if ( $username == 'qgrimaud' || $username == 'chandeli' || $username == 'fifre' || $username == 'pinna' || $username == 'sander' || $username == 'vactis' || $username == 'abourdin' ) // debug
				$mailbox = Imap::login($username,$password);
			if(!$mailbox) { $_SESSION['form'] = $_POST['login']; $_SESSION['errors']['global'] = 'Identifiants inconnus'; }
			Imap::close($mailbox);
		}
		// Test si le formulaire est validé
		if(isset($_SESSION['form'])) {
			// Le formulaire n'est pas valide
			$this->render('login');
			// Destruction des sessions pour le remplissage des champs
			unset($_SESSION['form']);
			unset($_SESSION['errors']);
		} else {
			// Le formulaire est valide
			$_SESSION['user']['username'] = $username;
			$_SESSION['user']['password'] = $password;
			$_SESSION['user']['mailboxes'] = Imap::getMailboxes();
			// Connexion automatique
			if(isset($remember)) {
				setcookie("username",User::perso_encrypt($username),time()+60*60*24*30, '/') or die('probleme lors du setcookie');
				setcookie("password",User::perso_encrypt($password),time()+60*60*24*30, '/') or die('probleme lors du setcookie');
			}
			header('Location: '.WEB_ROOT.'webmail/menu');
			exit();
		}
	}
	
	public function logout() {
		if(!User::isLogged()) {
			header('Location: '.WEB_ROOT.'webmail/index');
			exit();
		}
		unset($_SESSION['user']);
		setcookie("username",'',time()-3600, '/'); // temps en secondes
		setcookie("password",'',time()-3600, '/'); // temps en secondes
		header('Location: '.WEB_ROOT.'webmail/index');
		exit();
	}
	
}